package com.ddm.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;


/**
 * @author 丁丁猫
 * @version 1.0
 * 代码生成器具体配置
 */
public class Generator {
    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(setDataSourceConfig());
        autoGenerator.setGlobalConfig(setGlobalConfig());
        autoGenerator.setPackageInfo(setPackageConfig());
        autoGenerator.setStrategy(setStrategyConfig());
        autoGenerator.execute();
    }

    //设置数据源
    private static DataSourceConfig setDataSourceConfig() {
        DataSourceConfig dataSource = new DataSourceConfig();
        System.out.println(dataSource.getUsername());
        dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/standard_test_db?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("aba032463");
        return dataSource;

    }

    //设置全局配置
    private static GlobalConfig setGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/cx-server/src/main/java");
        globalConfig.setOpen(true);
        globalConfig.setAuthor("horizon");
        globalConfig.setServiceName("%sService");
        globalConfig.setFileOverride(true);
        globalConfig.setSwagger2(true);//开启Swagger2模式
        globalConfig.setIdType(IdType.AUTO);
        return globalConfig;
    }
    //设置包名相关设置
    private static PackageConfig setPackageConfig(){
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.ddm");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("entity");
        return packageConfig;
    }

    //策略设置
    private static StrategyConfig setStrategyConfig(){
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setLogicDeleteFieldName("deleted");
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig .setTableFillList(Arrays.asList(
                new TableFill("update_time", FieldFill.INSERT_UPDATE),
                new TableFill("update_user", FieldFill.INSERT_UPDATE),
                new TableFill("create_time", FieldFill.INSERT),
                new TableFill("create_user", FieldFill.INSERT)
        ));
        return strategyConfig;
    }
}
